package com.zc.mychart.model;

import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.List;

public class DKRadarEntrys {

    private List<Entry> As;
    private List<Entry> Bs;
    private List<Entry> CS;

    private final int M = 5;
    private final int N = 20;

    private List<Float> m5CList;
    private List<Float> m20CList;
    List<CandleEntry> transformList;
    List<Entry> transformK;
    List<Entry> transformD;


    public DKRadarEntrys(List<CandleEntry> list){
        float mAC = 0.0f;
        float mBC = 0.0f;
        boolean lastBoolean = true;
        boolean currentBoolean = true;
        String drawKD = "";
        float mCC;
        As = new ArrayList<>();
        Bs = new ArrayList<>();
        CS = new ArrayList<>();
        m5CList = new ArrayList<>();
        m20CList = new ArrayList<>();
        transformList = new ArrayList<>();
        transformK = new ArrayList<>();
        transformD = new ArrayList<>();

        for (int i = 0; i < list.size(); i++){
            CandleEntry entry = list.get(i);
            if(m5CList.size() <= M){
                m5CList.add(entry.getClose());
            }else{
                m5CList.remove(0);
                m5CList.add(entry.getClose());
            }

            if(m20CList.size() <= N){
                m20CList.add(entry.getClose());
            }else{
                m20CList.remove(0);
                m20CList.add(entry.getClose());
            }

            if(m5CList.size() == M + 1){
                mAC = getMAC(m5CList,M);
            }

            if(m20CList.size() == N + 1){
                mBC = getMAC(m20CList,N);
            }

            float x = entry.getX();

            if(mBC > 0){
                currentBoolean = mAC > mBC;
            }

            if(currentBoolean != lastBoolean && mBC > 0){
                if(!currentBoolean){
                    drawKD = "K";
                    transformK.add(new Entry(x, (float) (entry.getHigh() + entry.getHigh() * 0.001)));
                }else{
                    drawKD = "D";
                    transformD.add(new Entry(x, (float) (entry.getLow() - entry.getLow() * 0.001)));
                }
            }

            EntryData data = new EntryData(currentBoolean,drawKD);
            data.time = ((EntryData)entry.getData()).time;

//            if(mAC > 0){
//                As.add(new Entry(x, mAC));
//            }
//
//            if(mBC > 0){
//                Bs.add(new Entry(x, mBC));
//            }

            As = getMAEntries(list,M);
            Bs = getMAEntries(list,N);
            //entry.setData(data);

//            Float.parseFloat(item.getHigh()),
//                    Float.parseFloat(item.getLow()),
//                    Float.parseFloat(item.getOpen()),
//                    Float.parseFloat(item.getClose()),

            CandleEntry entry1;

            if(mAC >= mBC){
                entry1 = new CandleEntry(
                        x,
                        entry.getHigh(),
                        entry.getLow(),
                        entry.getLow(),
                        entry.getHigh(),
                        data
                );
            }else{
                entry1 = new CandleEntry(
                        x,
                        entry.getHigh(),
                        entry.getLow(),
                        entry.getHigh(),
                        entry.getLow(),
                        data
                );
            }

            transformList.add(entry1);

            lastBoolean = currentBoolean;
        }
    }



    public Float getMAC(List<Float> list, int days) {
        float sum = 0;
        if (list != null) {
            for (int i = 0 ; i < list.size() - 1; i++) {
                sum += list.get(i);
            }
        }
        return sum / days;
    }

//    public Float getMAC(List<CandleEntry> list, int days) {
//        if (list != null) {
//            for (int i = days - 1; i < list.size(); i++) {
//                float sum = 0;
//                for (int j = 0; j < days; j++) {
//                    sum += list.get(i - j).getClose();
//                }
//                return sum / days;
//            }
//        }
//        return 0.0f;
//    }

    private List<Entry> getMAEntries(List<CandleEntry> list, int n) {
        List<Entry> entries = new ArrayList<>();
        if (list != null) {
            for (int i = n - 1; i < list.size(); i++) {
                float sum = 0;
                for (int j = 0; j < n; j++) {
                    sum += list.get(i - j).getClose();
                }
                Entry entry = new Entry(list.get(i).getX(), sum / n);
                entries.add(entry);
            }
        }
        return entries;
    }

    public List<Entry> getAs() {
        return As;
    }

    public List<Entry> getBs() {
        return Bs;
    }

    public List<CandleEntry> getTransformList() {
        return transformList;
    }

    public List<Entry> getTransformK() {
        return transformK;
    }

    public List<Entry> getTransformD() {
        return transformD;
    }
}
